package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.*;
import cn.wolfcode.wms.exception.LogicException;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.SaleAccountMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;

@Service
public class ProductStockService implements IProductStockService {


    @Autowired
    ProductStockMapper productStockMapper;
    @Autowired
    private SaleAccountMapper saleAccountMapper;

    //入库操作
    public void stockIncome(StockIncomeBill stockincomebill) {
        for(StockIncomebillItem item :stockincomebill.getItems()){
            //根据,商品, + 仓库 查询对应的库存记录
            ProductStock ps = productStockMapper.selectByProductIdAndDepotId(item.getProduct().getId() ,stockincomebill.getDepot().getId());
            //判断是否有库存记录,如果没有就新增一条库存记录
            if( ps == null){
                ps = new ProductStock();
                ps.setProduct(item.getProduct());
                ps.setDepot(stockincomebill.getDepot());
                ps.setPrice(item.getCostPrice());
                ps.setStoreNumber(item.getNumber());
                ps.setAmount(item.getAmount());
                productStockMapper.insert(ps);
            }else{
                //否则更新库存记录的数量 ,价格,总额
                ps.setStoreNumber(ps.getStoreNumber().add(item.getNumber()));
                ps.setAmount(ps.getAmount().add(item.getAmount()));
                ps.setPrice(item.getAmount().divide(ps.getStoreNumber(),2, RoundingMode.HALF_UP));
                productStockMapper.updateByPrimaryKey(ps);

            }

        }

    }
    //出库操作
    public void stockOutcome(StockOutcomeBill stockoutcomeebill) {


        for (StockOutcomebillItem item : stockoutcomeebill.getItems()) {
            //1.根据商品id+仓库id查询储存记录
            ProductStock ps = productStockMapper.selectByProductIdAndDepotId(item.getProduct().getId(), stockoutcomeebill.getDepot().getId());
            //2.判断如果 ps  == null 直接提示用户xx仓库中没有XX货品\
            if(ps == null){
                throw  new LogicException(stockoutcomeebill.getDepot().getName()+"中没有"+item.getProduct().getName());

            }
            //3.如果有库存 ,在判断商品数量是否大于这次出库数量
            //4.如果不够直接提示用户 xx货品在xx仓库数量不足
            if(ps.getStoreNumber().compareTo(item.getNumber() )<0){
                throw  new LogicException(item.getProduct().getName()+"在"+ps.getDepot().getName()+"数量足");
            }

            //5.商品数量充足
            //6.将库存明细的数量减去储气库数量
            ps.setStoreNumber(ps.getStoreNumber().subtract(item.getNumber()));
            //7.重新计算库存总额
            ps.setAmount(ps.getPrice().multiply(ps.getStoreNumber()).setScale(2,RoundingMode.HALF_UP));
            //8.跟新库存明细
            productStockMapper.updateByPrimaryKey(ps);

            //记录销售账单
            SaleAccount sa = new SaleAccount();
            sa.setClientId(stockoutcomeebill.getClient().getId());
            sa.setCostPrice(ps.getPrice());
            sa.setNumber(item.getNumber());
            sa.setSalePrice(item.getSalePrice());
            sa.setCostAmount(sa.getNumber().multiply(sa.getCostPrice().setScale(2,RoundingMode.HALF_UP)));
            sa.setProductId(item.getProduct().getId());
            sa.setSaleManId(stockoutcomeebill.getInputUser().getId());
            sa.setVdate(stockoutcomeebill.getVdate());
            //保存销售账单
            saleAccountMapper.insert(sa);

        }

    }

    public PageResult query(QueryObject qo) {
        int count = productStockMapper.queryForCount(qo);
        if(count == 0){
            return PageResult.empty(qo.getPageSize());
        }
        return new PageResult(qo.getPageSize(),count,qo.getCurrentPage(),productStockMapper.quertForList(qo));
    }
}
